<%@ page language="java" import="java.util.*,java.sql.*"
	pageEncoding="UTF-8"%>
<html>
<head>
<title>登录</title>
</head>

<body>
	<%
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url1 = "jdbc:mysql://localhost:3306/user";
			String url2 = "?user=root&password=123456";
			String url3 = "&useUnicode=true&characterEncoding=UTF-8";
			String url = url1 + url2 + url3;
			Connection con = DriverManager.getConnection(url);
			request.setCharacterEncoding("UTF-8");
			String user = request.getParameter("user");
			session.setAttribute("user", user);
			String password = request.getParameter("password");
			if (!user.equals("") && !password.equals("")) {
				String sql = "select * from register where user=? and password=?";
				PreparedStatement pstmt = con.prepareStatement(sql);
				pstmt.setString(1, user);
				pstmt.setString(2, password);
				ResultSet rs = pstmt.executeQuery();
				rs.last();
				if (rs.getRow()>0) {
					if (rs != null)
						rs.close();
					if (pstmt != null)
						pstmt.close();
					if (con != null)
						con.close();
					response.sendRedirect("main_page.jsp");
				} else {
					out.print("账号密码不匹配！");
					if (rs != null)
						rs.close();
					if (pstmt != null)
						pstmt.close();
					if (con != null)
						con.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	%>
	<form action="login.jsp" method="post">
		<table style="margin: 100px auto;">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="user">
				</td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="password" name="password">
				</td>
			</tr>
			<tr>
				<td><input type="submit" value="提交">
				</td>
				<td><input type="reset" value="取消">
				</td>
			</tr>
		</table>
	</form>
	<a href="register.jsp">注册</a>
</body>
</html>
